<template>
  <div class="operation-logs">
    <el-card class="logs-card" shadow="hover">
      <div slot="header" class="card-header">
        <div class="header-left">
          <i class="el-icon-document"></i>
          <span>任务记录</span>
          <el-tooltip content="记录系统所有操作的执行情况" placement="right">
            <i class="el-icon-question"></i>
          </el-tooltip>
        </div>
      </div>

      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-form :inline="true" :model="searchForm" ref="searchForm" size="small">
          <el-form-item label="关键字：">
            <el-input
              v-model="searchForm.keyword"
              placeholder="请输入搜索内容"
              clearable
              style="width: 220px">
            </el-input>
          </el-form-item>
          <el-form-item label="选择日期：">
            <el-date-picker
              v-model="searchForm.dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              style="width: 340px">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 日志列表 -->
      <el-table
        :data="filteredTableData"
        style="width: 100%"
        border
        v-loading="loading"
        element-loading-text="加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        stripe>
        <el-table-column prop="enterprise" label="企业名称" min-width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.enterprise || '--' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="ipAddress" label="IP地址" min-width="140">
          <template slot-scope="scope">
            <span class="ip-address">{{ scope.row.ipAddress }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="operationTime" label="操作时间" min-width="180"></el-table-column>
        <el-table-column prop="eventType" label="事件类型" width="120">
          <template slot-scope="scope">
            <el-tag size="small" type="info">{{ scope.row.eventType }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="log" label="日志" min-width="300">
          <template slot-scope="scope">
            <div class="log-content" :class="{ 'error': scope.row.log.includes('失败') }">
              {{ scope.row.log }}
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <div class="pagination-container">
        <span class="total-count">共 {{ total }} 条数据</span>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'OperationLogs',
  data() {
    return {
      // 搜索相关
      searchForm: {
        keyword: '',
        dateRange: []
      },
      loading: false,

      // 表格数据
      tableData: [
        {
          enterprise: '--',
          ipAddress: '47.92.255.19',
          operationTime: '2023-12-06 18:23:00',
          eventType: '计划任务',
          log: '开机失败(服务器处于开机无需执行)'
        },
        {
          enterprise: '--',
          ipAddress: '39.99.234.143',
          operationTime: '2023-11-03 09:40:01',
          eventType: '计划任务',
          log: '关机成功'
        },
        {
          enterprise: '--',
          ipAddress: '39.98.90.201',
          operationTime: '2023-11-03 07:07:00',
          eventType: '计划任务',
          log: '关机失败(服务器处于关机无需执行)'
        }
      ],

      // 分页相关
      currentPage: 1,
      pageSize: 10,
      total: 10
    }
  },
  computed: {
    filteredTableData() {
      let result = [...this.tableData]

      // 关键字搜索
      if (this.searchForm.keyword) {
        const keyword = this.searchForm.keyword.toLowerCase()
        result = result.filter(item =>
          (item.enterprise && item.enterprise.toLowerCase().includes(keyword)) ||
          item.ipAddress.includes(keyword) ||
          item.log.toLowerCase().includes(keyword)
        )
      }

      // 日期范围筛选
      if (this.searchForm.dateRange && this.searchForm.dateRange.length === 2) {
        const startDate = new Date(this.searchForm.dateRange[0])
        const endDate = new Date(this.searchForm.dateRange[1])
        endDate.setHours(23, 59, 59, 999) // 设置结束时间为当天最后一毫秒

        result = result.filter(item => {
          const operationDate = new Date(item.operationTime)
          return operationDate >= startDate && operationDate <= endDate
        })
      }

      return result
    }
  },
  methods: {
    // 搜索相关方法
    handleSearch() {
      this.currentPage = 1
      this.getList()
    },
    resetSearch() {
      this.$refs.searchForm.resetFields()
      this.handleSearch()
    },

    // 分页相关方法
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    // 获取列表数据
    getList() {
      this.loading = true
      // 模拟API调用
      setTimeout(() => {
        this.loading = false
      }, 800)
    }
  },
  created() {
    this.getList()
  }
}
</script>

<style scoped>
.operation-logs {
  padding: 10px;
}

.logs-card {
  margin-bottom: 20px;
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-left i {
  font-size: 18px;
}

.header-left .el-icon-question {
  font-size: 14px;
  color: #909399;
  cursor: help;
}

.search-bar {
  margin-bottom: 20px;
  padding: 18px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.ip-address {
  font-family: monospace;
  color: #409EFF;
}

.log-content {
  color: #67C23A;
}

.log-content.error {
  color: #F56C6C;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.total-count {
  color: #606266;
  font-size: 14px;
}

/* 表格样式优化 */
.el-table {
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 20px;
}

.el-table th {
  background-color: #f5f7fa !important;
  color: #606266;
  font-weight: bold;
}

.el-table--striped .el-table__body tr.el-table__row--striped td {
  background-color: #fafafa;
}

/* 表单样式优化 */
.el-form--inline .el-form-item {
  margin-right: 20px;
}

.el-form--inline .el-form-item:last-child {
  margin-right: 0;
}

/* 标签样式 */
.el-tag {
  border-radius: 4px;
  padding: 0 8px;
  height: 24px;
  line-height: 22px;
}

/* 加载动画样式 */
.el-loading-spinner .el-icon-loading {
  font-size: 30px;
  color: #409EFF;
}

.el-loading-spinner .el-loading-text {
  color: #409EFF;
  margin: 3px 0;
  font-size: 14px;
}

/* 日期选择器样式 */
.el-date-editor .el-range-separator {
  padding: 0 5px;
}

.el-date-editor--daterange.el-input__inner {
  width: 340px;
}
</style>
